<script>
import DoctorSchedule from '@/views/home/DoctorSchedule.vue';
export default {
  name: 'UserInfo',
  components: {DoctorSchedule},
  data(){
    // 验证确认密码 是否一致
    const validatePass0 = (rule, value, callback) => {
      if (this.ruleForm.pass === this.ruleForm.oldPass) {
        callback(new Error('新密码不能和原密码相同'));
      } else {
        callback();
      }
    };

    // 自定义校验规则 验证密码
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    // 验证确认密码 是否一致
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      // 图片上传的请求头
      headers: {
        token: sessionStorage.getItem("token")
      },
      action: this.$http.defaults.baseURL + "/fileUpload/upload",

// 用户信息
      userInfo:{},
// 医生的排班表信息
      doctor: {
        name: '',
        department: '',
        schedule: [],
      },
// 值班日期
      dateRange: [],
// 门诊值班 和  急诊值班 的切换  1 为门诊值班  2 为急诊值班
      value1: 1,

//   修改密码
      dialogVisible:false,
      ruleForm: {
        oldPass: '',
        pass: '',
        checkPass: '',
      },
      rules: {
        oldPass: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 3, max: 12, message: '密码长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        pass: [
          {required: true, validator: validatePass, trigger: 'blur' },
          {required: true, validator: validatePass0, trigger: 'blur' },
          { min: 3, max: 12, message: '密码长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        checkPass: [
          {required: true, validator: validatePass2, trigger: 'blur' }
        ],
      }
    }
    },
  created() {
    this.loadScheduling()
  },
  methods:{
    //上传图片
    handleAvatarSuccess(res, file) {
      this.userInfo.picture = res.data
      this.$http.post('/user/edit',this.userInfo).then(resp=>{
        //修改用户头像信息
      })
    },
    //上传图片
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 关闭和取消修改密码
    handleClose() {
      this.dialogVisible = false;
      // 清空表单
      this.$refs.ruleForm.resetFields();
    },
    //确认修改密码
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm)
          this.$http.post('/user/updatePassword',this.ruleForm).then(resp=>{
            if(resp.data.code===200){
              this.dialogVisible = false;
              //修改成功提示三秒后下线
              this.$message({
                message: '修改成功，即将退出登录',
                type: 'success',
                duration: 3000,
                onClose: () => {
                  this.$http.get("/userAuth/logout").then(resp=>{
                    if(resp.data.code===200){
                      this.$message.success(resp.data.data);
                      sessionStorage.clear();
                      this.$router.push("/login");
                    }else {
                      this.$message.error("退出失败");
                    }
                  })
                }
              })
            }
          })
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 加载值班信息
    loadScheduling(){
      this.$http.get('/hisScheduling/getSchedulingBySubsectionType/'+this.value1).then(resp => {
        console.log(resp)
        this.doctor.schedule = resp.data.data.schedule;
        this.doctor.name = resp.data.data.name;
        this.doctor.department = resp.data.data.department;
        this.dateRange = resp.data.data.dateRange;
      })
    },

    // 密码 更改
    passwordUpdate(){
      this.dialogVisible = true;
    },
    // 加载用户信息
    loadData() {
      this.$http.get('/user/info').then(resp => {
        this.userInfo = resp.data.data;
      })
    },
    changeType(){
      this.loadScheduling()
    },
  },
  mounted() {
    this.loadData()
  },

}
</script>

<template>
  <div class="app">
    <div class="header">
      <div class="tx">
        <el-upload
        :headers="headers"
        :action="action"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img :src="userInfo.picture" class="txImg">
        </el-upload>
      </div>
      <div class="content">
        <h1>{{ userInfo.userName }}</h1>
        <p>年龄: {{ userInfo.age }}</p>
        <p>电话: {{ userInfo.phone }}</p>
        <p>邮箱: {{ userInfo.email }}</p>
        <p>简介：{{ userInfo.introduction }}</p>
      </div>
      <div class="btn_edit">
        <el-button round type="info" icon="el-icon-edit"  @click="passwordUpdate()">
          修改密码
        </el-button>
      </div>
      </div>
<!--      两个时间按钮-->
    <div style="margin-left: 20px">
      <el-switch
          @change="changeType"
          v-model="value1"
          active-text="急诊值班"
          active-value="2"
          inactive-text="门诊值班"
          inactive-value="1"
          inactive-color="#40ff90">
      </el-switch>
    </div>
      <DoctorSchedule :doctor="doctor" :dateRange="dateRange"/>
    <!--  修改密码-->
    <el-dialog
        title="修改密码"
        :visible.sync="dialogVisible"
        @close="handleClose"
    >
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="原密码" prop="oldPass">
          <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style>
*{
  font-size: 16px;
}
.header {
  /* border: 1px solid yellowgreen; */
  display: flex;
}
.tx {
  height: 200px;
  width: 200px;
  /* border: 1px solid pink; */
  margin: 10px 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.txImg {
  width: 200px;
  height: 200px;
}

.content {
  width: 70%;
  margin: 030px;
  /* border: 1px solid pink; */
}
.btn_edit {
  height: 50px;
  width: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>